package com.hm_.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hm.model.User;

//项目/模块路径/方法路径
@Controller //=component
@RequestMapping("/hello")
public class HelloController {

//	@RequestMapping(value="/{addr}/info",method=RequestMethod.POST, params= {"name","age!=10"},headers= {"User-Agent"})
	@GetMapping(value = "/{addr}/info", headers = { "User-Agent" })
	@ResponseBody
	public String info(@PathVariable("addr") String abc, @RequestHeader("User-Agent") String ua) {
		return "<h1>这里是一个独立的controller文件" + ua + "</h1>";
	}

	@GetMapping("testcook")
	@ResponseBody
	public String cook(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		String cv = "";
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals("111")) {
				cv = cookie.getValue();
			}
		}
		return cv;
	}

	@GetMapping("testcook2")
	@ResponseBody
	public String cook2(@CookieValue("111") String sv) {
		return sv;
	}

	@GetMapping("login")
	public @ResponseBody Object login(@Valid User user,Map<String, Object> map) {
		user.setUserName("刘德华");
		map.put("user", user); //进入了request作用域
		return user;
	}

}
